package com.soft.sitegenerator.action;

import java.util.Collection;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.soft.sitegenerator.Component;
import com.soft.sitegenerator.DataBaseComponent;
import com.soft.sitegenerator.business.hibernate.User;
import com.soft.sitegenerator.form.MemberListForm;

public class MemberListAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		MemberListForm memberListForm = (MemberListForm) form;

		Component component = new DataBaseComponent();
		User member = (User) component.getBean("usersHib");

		memberListForm.setUsers(member.findAll());

		memberListForm.setMostrar(buscarGrupos(memberListForm.getUsers()));

		return mapping.findForward("showList");
	}

	private boolean buscarGrupos(Collection users) {

		boolean existe = false;

		for (Iterator it = users.iterator(); it.hasNext() && !existe;)
			if (!((User) it.next()).getNewsGroups().isEmpty())
				existe = true;

		return existe;
	}

}